const router = require('koa-router')()
const jsonwebtoken = require('jsonwebtoken')
const { SuccessModel, ErrorModel } = require('../model/resModel')
const {
  signup,
  login,
  changeAvatar,
  changeUsername,
  getUserLikes
} = require('../controller/user')
const checkToken = require('../middleware/checkToken')

const secret = 'Peppa_pig' // 密钥

router.post('/user/signup', async (ctx, next) => {
  const postData = ctx.request.body
  const result = await signup(postData)
  if (result) {
    ctx.body = new SuccessModel('注册成功🎉')
  } else {
    ctx.body = new ErrorModel('用户已存在😮‍💨')
  }
})

router.get('/user/current', checkToken, async (ctx, next) => {
  ctx.body = new SuccessModel(ctx.state.token, '登录成功🎉')
})

router.post('/user/login', async (ctx, next) => {
  const postData = ctx.request.body
  const result = await login(postData)
  if (result) {
    const token = jsonwebtoken.sign({ username: result.username, avatar: result.avatar }, secret, {
      expiresIn: '1h'
    })
    result.token = token
    ctx.body = new SuccessModel(result, '登录成功🎉') // result 中包含用户名和用户头像链接
  } else {
    ctx.body = new ErrorModel('登录失败😮‍💨')
  }
})

// 用户选择系统头像
router.post('/user/avatar/change', checkToken, async (ctx, next) => {
  const postData = ctx.request.body
  postData.avatar = `http://localhost:3000/${postData.avatar}`
  const result = await changeAvatar(postData)
  if (result) {
    // 当用户修改头像时，应该重新生成 token 返回给前端
    const token = jsonwebtoken.sign({ username: postData.username, avatar: postData.avatar }, secret, {
      expiresIn: '1h'
    })
    ctx.body = new SuccessModel({ avatar: postData.avatar, token: token }, '头像修改成功🎉')
  } else {
    ctx.body = new ErrorModel('头像修改失败😮‍💨')
  }
})

// 用户上传头像
router.post('/user/avatar/upload', checkToken, async (ctx, next) => {
  const filename = ctx.request.files.file.newFilename
  const username = ctx.request.body.username
  const userInfo = {
    username: username,
    avatar: `http://localhost:3000/uploads/${filename}`
  }
  const result = await changeAvatar(userInfo)
  if (result) {
    const token = jsonwebtoken.sign({ username: userInfo.username, avatar: userInfo.avatar }, secret, {
      expiresIn: '1h'
    })
    ctx.body = new SuccessModel({ avatar: userInfo.avatar, token: token }, '头像上传成功🎉')
  } else {
    ctx.body = new ErrorModel('头像上传失败😮‍💨')
  }
})

// 修改用户名
router.post('/user/username/change', checkToken, async (ctx, next) => {
  const postData = ctx.request.body
  const result = await changeUsername(postData)
  if (result) {
    const token = jsonwebtoken.sign({ username: result.username, avatar: result.avatar }, secret, {
      expiresIn: '1h'
    })
    ctx.body = new SuccessModel({token: token}, '用户名修改成功🎉')
  } else {
    ctx.body = new ErrorModel('用户名已存在😮‍💨')
  }
})

// 获取用户点赞过的留言
router.get('/user/likes', checkToken, async (ctx, next) => {
  const username = ctx.query.username
  const results = await getUserLikes(username)
  ctx.body = new SuccessModel(results, '查询成功🎉')
})

module.exports = router
